package klk.shop.objects;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

import klk.shop.utilities.HTMLUtilities;
import klk.shop.utilities.KCreditCardValidator;
import klk.shop.utilities.MysqlUtilities;

public class CreditCard extends KLKObject{
	
	protected int id;
	protected String cardNumber;
	protected Date expirationDate;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCardNumber() {
		return cardNumber;
	}
	public void setCardNumber(String cardNumber) {
		this.cardNumber = cardNumber;
	}
	
	public Date getExpirationDate() {
		return expirationDate;
	}
	public void setExpirationDate(Date expirationDate) {
		this.expirationDate = expirationDate;
	}
	
	public CreditCard(int id, String number, Date expirationDate) {
		super();
		this.id=id;
		this.cardNumber = number;
		this.expirationDate = expirationDate;
	}
	public CreditCard() {
		this(-1, "", new Date());
	}
	
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		String shortenedCardNumber=cardNumber.substring(0, cardNumber.length()-3)+"***";
		sb.append(shortenedCardNumber);
		sb.append(" (");
		sb.append(KCreditCardValidator.getBankName(cardNumber));
		sb.append(", ");
		sb.append((expirationDate.getMonth()+1));
		sb.append('/');
		sb.append((expirationDate.getYear()+1900));
		sb.append(")");
		return sb.toString();
	}
	
	@Override
	public String toHtml() {
		return toString();
	}
		
	public static ArrayList<CreditCard> getCustomersCreditCardListFromDatabase(int customerId) {
		ArrayList<CreditCard> creditCardList = new ArrayList<CreditCard>();
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
				
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(
				"SELECT " +
					"id, " +
					"card_number, " +
					"expiration_date " +
				"FROM " +
					"credit_cards " +
				"WHERE customer_id='"+customerId+"'; "
			);
			
			
			while (rs.next()) {
				creditCardList.add(
					new CreditCard(
						rs.getInt("id"),
						rs.getString("card_number"),
						rs.getDate("expiration_date")
					)
				);
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) try { rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		
		return creditCardList;	
	}
	
	public static int SetCreditCardIntoDatabase(CreditCard cc, Customer cu) {	
		int rowsAffected = 0;

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
	
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			String insertString = 
					"INSERT INTO `credit_cards` (" + 
						"customer_id, card_number, expiration_date " +
					") VALUES (" +
								cu.getCustomerId() + ", " +
						"'" + 	cc.getCardNumber() 		+ "', " +  
						"'" + 	MysqlUtilities.toMySqlDateString(cc.expirationDate) + "' " + 	  
					");";
			System.out.println(insertString);
			rowsAffected = conn.createStatement().executeUpdate( insertString ); 
			
			stmt = conn.createStatement();
			rs = stmt.executeQuery(
				"SELECT LAST_INSERT_ID();"
			);
			rs.next();
			cc.setId(rs.getInt("LAST_INSERT_ID()"));
			
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			if (  rs != null) try {   rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		return rowsAffected;	
	}
	
	
	@SuppressWarnings("deprecation")
	public static CreditCard tryParse(String cardNumber, String expirationDateMonth, String expirationDateYear) {
		CreditCard c = new CreditCard();
		try { c.cardNumber=cardNumber; } catch (Exception e) {}
		try { c.expirationDate.setMonth(Integer.parseInt(expirationDateMonth)-1); } catch (Exception e) {}
		try { c.expirationDate.setYear(Integer.parseInt(expirationDateYear)-1900); } catch (Exception e) {}
		return c;
	}
		
		
}
